package com.agileai.portal.controller.auth;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.BaseHandler;
import com.agileai.hotweb.domain.FormSelect;
import com.agileai.hotweb.domain.TreeBuilder;
import com.agileai.hotweb.domain.TreeModel;
import com.agileai.hotweb.domain.core.Resource;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.portal.bizmoduler.ColumnTreeManage;
import com.agileai.portal.bizmoduler.SecurityAuthorizationConfig;
import com.agileai.portal.driver.AttributeKeys;
import com.agileai.portal.driver.model.MenuBar;
import com.agileai.portal.driver.model.MenuItem;
import com.agileai.portal.driver.model.Page;
import com.agileai.portal.driver.model.PortletWindowConfig;
import com.agileai.portal.driver.service.DriverConfiguration;
import com.agileai.portal.driver.service.PortalConfigService;
import com.agileai.util.ListUtil;

/**
 * generated by miscdp
 */
public class SecurityAuthorizationConfigHandler extends BaseHandler{
	private PortalConfigService portalConfigService = null;
	public SecurityAuthorizationConfigHandler(){
		super();
	}
	
	public ViewRenderer prepareDisplay(DataParam param){
		String resourceType = param.get("resourceType");
		String resourceId = param.get("resourceId");
		
		FormSelect roleSelect = buildRoleSelect(resourceType, resourceId);
		this.setAttribute("roleList", roleSelect);
		
		FormSelect groupSelect = buildGroupSelect(resourceType, resourceId);
		this.setAttribute("groupList", groupSelect);
		
		FormSelect userSelect = buildUserSelect(resourceType, resourceId);
		this.setAttribute("userList", userSelect);
		
		this.setAttribute("resourceType", resourceType);
		this.setAttribute("resourceId", resourceId);
		return new LocalRenderer(getPage());
	}
	
	private FormSelect buildRoleSelect(String resourceType,String resourceId){
		List<DataRow> roleList = this.getService().retrieveRoleList(resourceType, resourceId);
		FormSelect roleSelect = new FormSelect();
		roleSelect.setKeyColumnName("ROLE_ID");
		roleSelect.setValueColumnName("ROLE_NAME");
		roleSelect.addHasBlankValue(false);
		roleSelect.putValues(roleList);
		return roleSelect;
	}
	private FormSelect buildUserSelect(String resourceType,String resourceId){
		List<DataRow> userList = this.getService().retrieveUserList(resourceType, resourceId);
		FormSelect userSelect = new FormSelect();
		userSelect.setKeyColumnName("USER_ID");
		userSelect.setValueColumnName("USER_NAME");
		userSelect.addHasBlankValue(false);
		userSelect.putValues(userList);
		return userSelect;
	}
	
	private FormSelect buildGroupSelect(String resourceType,String resourceId){
		List<DataRow> groupList = this.getService().retrieveGroupList(resourceType, resourceId);
		FormSelect groupSelect = new FormSelect();
		groupSelect.setKeyColumnName("GRP_ID");
		groupSelect.setValueColumnName("GRP_NAME");
		groupSelect.addHasBlankValue(false);
		groupSelect.putValues(groupList);
		return groupSelect;
	}
	
	@PageAction
	public ViewRenderer addUserAuthRelation(DataParam param){
		String responseText = "";
		String resourceType = param.get("resourceType");
		String resourceId = param.get("resourceId");
		String userIds = param.get("userIds");
		List<String> userIdList = new ArrayList<String>();
		ListUtil.addArrayToList(userIdList, userIds.split(","));
		
		if (portalConfigService == null){
			portalConfigService = this.getPortalConfigService(this.getDispatchServlet().getServletContext());
		}
		
		if (Resource.Type.Portlet.equals(resourceType)){
			String portletId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnAddPortletAuth(portletId, resourceTypes, resourceIds);
			
			getService().addUserAuthRelation(resourceTypes, resourceIds, userIdList);
		}
		else if (Resource.Type.Menu.equals(resourceType)){
			String menuItemId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnAddMenuAuth(menuItemId, resourceTypes, resourceIds);
			getService().addUserAuthRelation(resourceTypes, resourceIds, userIdList);
		}
		else if (Resource.Type.Navigater.equals(resourceType)){
			getService().addUserAuthRelation(resourceType, resourceId,userIdList);
		}
		else if (Resource.Type.InfoColumn.equals(resourceType)){
			String columnId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnAddColumnAuth(columnId, resourceTypes, resourceIds);
		
			getService().addUserAuthRelation(resourceTypes, resourceIds, userIdList);
		}
		else if (Resource.Type.Infomation.equals(resourceType)){
			getService().addUserAuthRelation(resourceType, resourceId,userIdList);
		}
		else if (Resource.Type.Application.equals(resourceType)){
			getService().addUserAuthRelation(resourceType, resourceId,userIdList);
		}
		FormSelect userSelect = buildUserSelect(resourceType, resourceId);
		responseText = userSelect.getScriptSyntax("userList");
		return new AjaxRenderer(responseText);
	}
	
	private void packResoucesOnAddPortletAuth(String portletId,List<String> resourceTypes,List<String> resourceIds){
		PortletWindowConfig portletWindowConfig = portalConfigService.getPortletWindowConfig(portletId);
		resourceTypes.add(Resource.Type.Portlet);
		resourceIds.add(portletId);
		
		String menuItemId = portletWindowConfig.getMenuId(); 
		MenuItem menuItem = portalConfigService.getMenuItem(menuItemId);
		resourceTypes.add(Resource.Type.Menu);
		resourceIds.add(menuItemId);
		this.buildMenuParent(menuItem, resourceTypes, resourceIds);
	}

	private void packResoucesOnAddColumnAuth(String columnId,List<String> resourceTypes,List<String> resourceIds){
        ColumnTreeManage service = this.lookupService(ColumnTreeManage.class);
        List<DataRow> menuRecords = service.findTreeRecords(new DataParam());
        TreeBuilder treeBuilder = new TreeBuilder(menuRecords, "COL_ID","COL_NAME", "COL_PID");
        TreeModel treeModel = treeBuilder.buildTreeModel();
        TreeModel currentModel = treeModel.getFullTreeMap().get(columnId);
        resourceTypes.add(Resource.Type.InfoColumn);
        resourceIds.add(columnId);
        
        this.buildColumnParent(currentModel, resourceTypes, resourceIds);
	}
	
	private void buildColumnParent(TreeModel currentModel,List<String> resourceTypes,List<String> resourceIds){
		if (currentModel.getParent() != null){
			TreeModel parent = currentModel.getParent();
			String parentColumnId = parent.getId();
			resourceTypes.add(Resource.Type.InfoColumn);
			resourceIds.add(parentColumnId);
			
			this.buildColumnParent(parent, resourceTypes, resourceIds);
		}
	}
	
	private void packResoucesOnAddMenuAuth(String menuItemId,List<String> resourceTypes,List<String> resourceIds){
		MenuItem menuItem = portalConfigService.getMenuItem(menuItemId);
		
		resourceTypes.add(Resource.Type.Menu);
		resourceIds.add(menuItemId);
		this.buildMenuParent(menuItem, resourceTypes, resourceIds);
	}
	private void buildMenuParent(MenuItem menuItem,List<String> resourceTypes,List<String> resourceIds){
		if (menuItem.getParent() != null){
			MenuItem parentMenuItem = menuItem.getParent();
			resourceTypes.add(Resource.Type.Menu);
			resourceIds.add(parentMenuItem.getMenuId());
			
			this.buildMenuParent(parentMenuItem, resourceTypes, resourceIds);
		}
		else{
			MenuBar menuBar = menuItem.getMenuBar();
			resourceIds.add(menuBar.getId());
			resourceTypes.add(Resource.Type.Navigater);
		}
	}
	
	private PortalConfigService getPortalConfigService(ServletContext servletContext){
		DriverConfiguration driverConfiguration = (DriverConfiguration)servletContext.getAttribute(AttributeKeys.DRIVER_CONFIG);
		return driverConfiguration.getPortalConfigService();
	}
	
	@PageAction
	public ViewRenderer addRoleAuthRelation(DataParam param){
		String responseText = "";
		String resourceType = param.get("resourceType");
		String resourceId = param.get("resourceId");
		String roleIds = param.get("roleIds");
		List<String> roleIdList = new ArrayList<String>();
		ListUtil.addArrayToList(roleIdList, roleIds.split(","));
		
		if (portalConfigService == null){
			portalConfigService = this.getPortalConfigService(this.getDispatchServlet().getServletContext());
		}
		
		if (Resource.Type.Portlet.equals(resourceType)){
			String portletId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnAddPortletAuth(portletId, resourceTypes, resourceIds);
			
			getService().addRoleAuthRelation(resourceTypes, resourceIds, roleIdList);
		}
		else if (Resource.Type.Menu.equals(resourceType)){
			String menuItemId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnAddMenuAuth(menuItemId, resourceTypes, resourceIds);
			
			getService().addRoleAuthRelation(resourceTypes, resourceIds, roleIdList);
		}
		else if (Resource.Type.Navigater.equals(resourceType)){
			getService().addRoleAuthRelation(resourceType, resourceId,roleIdList);
		}
		
		else if (Resource.Type.InfoColumn.equals(resourceType)){
			String columnId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnAddColumnAuth(columnId, resourceTypes, resourceIds);
			
			getService().addRoleAuthRelation(resourceTypes, resourceIds, roleIdList);
		}
		else if (Resource.Type.Infomation.equals(resourceType)){
			getService().addRoleAuthRelation(resourceType, resourceId,roleIdList);
		}
		else if (Resource.Type.Application.equals(resourceType)){
			getService().addRoleAuthRelation(resourceType, resourceId,roleIdList);
		}
		FormSelect roleSelect = buildRoleSelect(resourceType, resourceId);
		responseText = roleSelect.getScriptSyntax("roleList");
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer addGroupAuthRelation(DataParam param){
		String responseText = "";
		String resourceType = param.get("resourceType");
		String resourceId = param.get("resourceId");
		String groupIds = param.get("groupIds");
		List<String> groupIdList = new ArrayList<String>();
		ListUtil.addArrayToList(groupIdList, groupIds.split(","));
		
		if (portalConfigService == null){
			portalConfigService = this.getPortalConfigService(this.getDispatchServlet().getServletContext());
		}
		
		if (Resource.Type.Portlet.equals(resourceType)){
			String portletId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnAddPortletAuth(portletId, resourceTypes, resourceIds);
			
			getService().addGroupAuthRelation(resourceTypes, resourceIds, groupIdList);
		}
		else if (Resource.Type.Menu.equals(resourceType)){
			String menuItemId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnAddMenuAuth(menuItemId, resourceTypes, resourceIds);
			
			getService().addGroupAuthRelation(resourceTypes, resourceIds, groupIdList);
		}
		else if (Resource.Type.Navigater.equals(resourceType)){
			getService().addGroupAuthRelation(resourceType, resourceId,groupIdList);
		}
		
		else if (Resource.Type.InfoColumn.equals(resourceType)){
			String columnId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnAddColumnAuth(columnId, resourceTypes, resourceIds);
			
			getService().addGroupAuthRelation(resourceTypes, resourceIds, groupIdList);			
		}
		else if (Resource.Type.Infomation.equals(resourceType)){
			getService().addGroupAuthRelation(resourceType, resourceId,groupIdList);
		}
		else if (Resource.Type.Application.equals(resourceType)){
			getService().addGroupAuthRelation(resourceType, resourceId,groupIdList);
		}		
		
		FormSelect groupSelect = buildGroupSelect(resourceType, resourceId);
		responseText = groupSelect.getScriptSyntax("groupList");
		return new AjaxRenderer(responseText);
	}
	
	
	@PageAction
	public ViewRenderer delUserAuthRelation(DataParam param){
		String userId = param.get("userList");
		String resourceType = param.get("resourceType");
		String resourceId = param.get("resourceId");
		
		if (portalConfigService == null){
			portalConfigService = this.getPortalConfigService(this.getDispatchServlet().getServletContext());
		}
		if (Resource.Type.Portlet.equals(resourceType)){
			getService().delUserAuthRelation(resourceType, resourceId, userId);			
		}
		else if (Resource.Type.Menu.equals(resourceType)){
			String menuItemId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnDelMenuAuth(menuItemId, resourceTypes, resourceIds);
			
			getService().delUserAuthRelation(resourceTypes, resourceIds, userId);
		}
		else if (Resource.Type.Navigater.equals(resourceType)){
			String menuBarId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnDelNavigaterAuth(menuBarId, resourceTypes, resourceIds);
			
			getService().delUserAuthRelation(resourceTypes, resourceIds, userId);
		}
		else if (Resource.Type.InfoColumn.equals(resourceType)){
			String columnId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnDelColumnAuth(columnId, resourceTypes, resourceIds);
			
			getService().delUserAuthRelation(resourceTypes, resourceIds, userId);
		}
		else if (Resource.Type.Infomation.equals(resourceType)){
			getService().delUserAuthRelation(resourceType, resourceId, userId);			
		}
		else if (Resource.Type.Application.equals(resourceType)){
			getService().delUserAuthRelation(resourceType, resourceId, userId);			
		}
		return this.prepareDisplay(param);
	}
	
	private void packResoucesOnDelMenuAuth(String menuItemId,List<String> resourceTypes,List<String> resourceIds){
		resourceTypes.add(Resource.Type.Menu);
		resourceIds.add(menuItemId);
		
		MenuItem menuItem = portalConfigService.getMenuItem(menuItemId);
		this.buildMenuChildren(menuItem, resourceTypes, resourceIds);
	}
	
	
	private void packResoucesOnDelColumnAuth(String columnId,List<String> resourceTypes,List<String> resourceIds){
        ColumnTreeManage service = this.lookupService(ColumnTreeManage.class);
        List<DataRow> menuRecords = service.findTreeRecords(new DataParam());
        TreeBuilder treeBuilder = new TreeBuilder(menuRecords, "COL_ID","COL_NAME", "COL_PID");
        TreeModel treeModel = treeBuilder.buildTreeModel();
        TreeModel currentModel = treeModel.getFullTreeMap().get(columnId);
        resourceTypes.add(Resource.Type.InfoColumn);
        resourceIds.add(columnId);
        
		this.buildColumnChildren(currentModel, resourceTypes, resourceIds);
	}
	
	private void buildColumnChildren(TreeModel currentModel,List<String> resourceTypes,List<String> resourceIds) {
		List<TreeModel> childList = currentModel.getChildren();
		for (int i=0;i < childList.size();i++){
			TreeModel tempTreeModel = childList.get(i);
			String columnId = tempTreeModel.getId();
			resourceIds.add(columnId);
			resourceTypes.add(Resource.Type.InfoColumn);
			
			buildColumnChildren(tempTreeModel, resourceTypes, resourceIds);
		}
	}
	
	
	private void packResoucesOnDelNavigaterAuth(String menuBarId,List<String> resourceTypes,List<String> resourceIds){
		resourceIds.add(menuBarId);
		resourceTypes.add(Resource.Type.Navigater);
		
		MenuBar menuBar = portalConfigService.getMenuBar(menuBarId);
		List<MenuItem> menuItems = menuBar.getMenuItemList();
		
		for (int i=0;i < menuItems.size();i++){
			MenuItem menuItem = menuItems.get(i);
			String menuItemId = menuItem.getMenuId();
			
			resourceIds.add(menuItemId);
			resourceTypes.add(Resource.Type.Menu);
			
			buildMenuChildren(menuItem, resourceTypes, resourceIds);
		}
	}
	
	private void buildMenuChildren(MenuItem menuItem,List<String> resourceTypes,List<String> resourceIds){
		if (menuItem.getChildren().size() > 0){
			List<MenuItem> menuItems = menuItem.getChildren();
			for (int i=0;i < menuItems.size();i++){
				MenuItem childMenuItem = menuItems.get(i);
				String childMenuItemId = childMenuItem.getMenuId();
				resourceTypes.add(Resource.Type.Menu);
				resourceIds.add(childMenuItemId);
				
				this.buildMenuChildren(childMenuItem, resourceTypes, resourceIds);
			}
		}else{
			String pageId = menuItem.getPageId();
			Page page = portalConfigService.getPage(pageId);
			HashMap<String, PortletWindowConfig> portletMap =  page.getPortletWindowConfigs();
			Iterator<String> iter = portletMap.keySet().iterator();
			
			while (iter.hasNext()){
				String portletId = iter.next();
				resourceTypes.add(Resource.Type.Portlet);
				resourceIds.add(portletId);				
			}
		}
	}
	
	@PageAction
	public ViewRenderer delRoleAuthRelation(DataParam param){
		String roleId = param.get("roleList");
		String resourceType = param.get("resourceType");
		String resourceId = param.get("resourceId");
		
		if (portalConfigService == null){
			portalConfigService = this.getPortalConfigService(this.getDispatchServlet().getServletContext());
		}
		if (Resource.Type.Portlet.equals(resourceType)){
			getService().delRoleAuthRelation(resourceType, resourceId, roleId);			
		}
		else if (Resource.Type.Menu.equals(resourceType)){
			String menuItemId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnDelMenuAuth(menuItemId, resourceTypes, resourceIds);
			
			getService().delRoleAuthRelation(resourceTypes, resourceIds, roleId);
		}
		else if (Resource.Type.Navigater.equals(resourceType)){
			String menuBarId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnDelNavigaterAuth(menuBarId, resourceTypes, resourceIds);
			getService().delRoleAuthRelation(resourceTypes, resourceIds, roleId);
		}
		else if (Resource.Type.InfoColumn.equals(resourceType)){
			String columnId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnDelColumnAuth(columnId, resourceTypes, resourceIds);
			
			getService().delRoleAuthRelation(resourceTypes, resourceIds, roleId);
		}
		else if (Resource.Type.Infomation.equals(resourceType)){
			getService().delRoleAuthRelation(resourceType, resourceId, roleId);			
		}
		else if (Resource.Type.Application.equals(resourceType)){
			getService().delRoleAuthRelation(resourceType, resourceId, roleId);			
		}		
		return this.prepareDisplay(param);
	}
	
	@PageAction
	public ViewRenderer delGroupAuthRelation(DataParam param){
		String groupId = param.get("groupList");
		String resourceType = param.get("resourceType");
		String resourceId = param.get("resourceId");
		
		if (portalConfigService == null){
			portalConfigService = this.getPortalConfigService(this.getDispatchServlet().getServletContext());
		}
		if (Resource.Type.Portlet.equals(resourceType)){
			getService().delGroupAuthRelation(resourceType, resourceId, groupId);			
		}
		else if (Resource.Type.Menu.equals(resourceType)){
			String menuItemId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnDelMenuAuth(menuItemId, resourceTypes, resourceIds);
			
			getService().delGroupAuthRelation(resourceTypes, resourceIds, groupId);
		}
		else if (Resource.Type.Navigater.equals(resourceType)){
			String menuBarId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnDelNavigaterAuth(menuBarId, resourceTypes, resourceIds);
			getService().delGroupAuthRelation(resourceTypes, resourceIds, groupId);
		}
		else if (Resource.Type.InfoColumn.equals(resourceType)){
			String columnId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnDelColumnAuth(columnId, resourceTypes, resourceIds);
			
			getService().delGroupAuthRelation(resourceTypes, resourceIds, groupId);
		}
		else if (Resource.Type.Infomation.equals(resourceType)){
			getService().delGroupAuthRelation(resourceType, resourceId, groupId);			
		}
		else if (Resource.Type.Application.equals(resourceType)){
			getService().delGroupAuthRelation(resourceType, resourceId, groupId);			
		}		
		return this.prepareDisplay(param);
	}	
	
	
	@PageAction
	public ViewRenderer delUserAuthRelations(DataParam param){
		String resourceType = param.get("resourceType");
		String resourceId = param.get("resourceId");
		
		if (portalConfigService == null){
			portalConfigService = this.getPortalConfigService(this.getDispatchServlet().getServletContext());
		}
		if (Resource.Type.Portlet.equals(resourceType)){
			getService().delUserAuthRelations(resourceType, resourceId);			
		}
		else if (Resource.Type.Menu.equals(resourceType)){
			String menuItemId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnDelMenuAuth(menuItemId, resourceTypes, resourceIds);
			
			getService().delUserAuthRelations(resourceTypes, resourceIds);
		}
		else if (Resource.Type.Navigater.equals(resourceType)){
			String menuBarId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnDelNavigaterAuth(menuBarId, resourceTypes, resourceIds);
			getService().delUserAuthRelations(resourceTypes, resourceIds);
		}
		
		else if (Resource.Type.InfoColumn.equals(resourceType)){
			String columnId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnDelColumnAuth(columnId, resourceTypes, resourceIds);
			
			getService().delUserAuthRelations(resourceTypes, resourceIds);
		}
		else if (Resource.Type.Infomation.equals(resourceType)){
			getService().delUserAuthRelations(resourceType, resourceId);			
		}
		else if (Resource.Type.Application.equals(resourceType)){
			getService().delUserAuthRelations(resourceType, resourceId);			
		}		
		return this.prepareDisplay(param);
	}
	
	@PageAction
	public ViewRenderer delRoleAuthRelations(DataParam param){
		String resourceType = param.get("resourceType");
		String resourceId = param.get("resourceId");
		
		if (portalConfigService == null){
			portalConfigService = this.getPortalConfigService(this.getDispatchServlet().getServletContext());
		}
		if (Resource.Type.Portlet.equals(resourceType)){
			getService().delRoleAuthRelations(resourceType, resourceId);			
		}
		else if (Resource.Type.Menu.equals(resourceType)){
			String menuItemId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnDelMenuAuth(menuItemId, resourceTypes, resourceIds);
			
			getService().delRoleAuthRelations(resourceTypes, resourceIds);
		}
		else if (Resource.Type.Navigater.equals(resourceType)){
			String menuBarId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnDelNavigaterAuth(menuBarId, resourceTypes, resourceIds);
			getService().delRoleAuthRelations(resourceTypes, resourceIds);
		}
		else if (Resource.Type.InfoColumn.equals(resourceType)){
			String columnId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnDelColumnAuth(columnId, resourceTypes, resourceIds);
			
			getService().delRoleAuthRelations(resourceTypes, resourceIds);
		}
		else if (Resource.Type.Infomation.equals(resourceType)){
			getService().delRoleAuthRelations(resourceType, resourceId);			
		}
		else if (Resource.Type.Application.equals(resourceType)){
			getService().delRoleAuthRelations(resourceType, resourceId);			
		}		
		return this.prepareDisplay(param);
	}
	
	@PageAction
	public ViewRenderer delGroupAuthRelations(DataParam param){
		String resourceType = param.get("resourceType");
		String resourceId = param.get("resourceId");
		
		if (portalConfigService == null){
			portalConfigService = this.getPortalConfigService(this.getDispatchServlet().getServletContext());
		}
		if (Resource.Type.Portlet.equals(resourceType)){
			getService().delGroupAuthRelations(resourceType, resourceId);			
		}
		else if (Resource.Type.Menu.equals(resourceType)){
			String menuItemId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnDelMenuAuth(menuItemId, resourceTypes, resourceIds);
			
			getService().delGroupAuthRelations(resourceTypes, resourceIds);
		}
		else if (Resource.Type.Navigater.equals(resourceType)){
			String menuBarId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnDelNavigaterAuth(menuBarId, resourceTypes, resourceIds);
			
			getService().delGroupAuthRelations(resourceTypes, resourceIds);
		}
		
		else if (Resource.Type.InfoColumn.equals(resourceType)){
			String columnId = resourceId;
			List<String> resourceTypes = new ArrayList<String>();
			List<String> resourceIds = new ArrayList<String>();
			this.packResoucesOnDelColumnAuth(columnId, resourceTypes, resourceIds);
			
			getService().delGroupAuthRelations(resourceTypes, resourceIds);
		}
		else if (Resource.Type.Infomation.equals(resourceType)){
			getService().delGroupAuthRelations(resourceType, resourceId);			
		}
		else if (Resource.Type.Application.equals(resourceType)){
			getService().delGroupAuthRelations(resourceType, resourceId);			
		}		
		return this.prepareDisplay(param);
	}	
	
    protected SecurityAuthorizationConfig getService() {
        return (SecurityAuthorizationConfig) this.lookupService(SecurityAuthorizationConfig.class);
    }
}
